package com.borrowed.book.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.borrowed.book.book.constant.BookConstant;
import com.borrowed.book.book.domain.CardOrderDetailDto;
import com.borrowed.book.book.domain.SelectPayOrderOutDto;
import com.borrowed.book.book.mapper.SPayOrderDetailMapper;
import com.borrowed.book.book.mapper.SPayOrderMapper;
import com.borrowed.book.book.service.WxOrderService;
import com.borrowed.book.coupon.service.SCouponUserService;
import com.borrowed.book.membercard.service.SMemberCardService;
import com.borrowed.book.user.mapper.SUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WxOrderServiceImpl implements WxOrderService {
    public static final Logger log = LoggerFactory.getLogger(com.borrowed.book.book.service.impl.WxOrderServiceImpl.class);

    @Autowired
    private SPayOrderMapper orderMapper;
    @Autowired
    private SCouponUserService couponUserService;
    @Autowired
    private SMemberCardService memCardService;
    @Autowired
    private SPayOrderDetailMapper payOrderDetailMapper;
    @Autowired
    private SUserMapper userMapper;


    /**
     * 更新订单状态，支付结束后调用更新为待发货状态
     * 更新优惠券状态，支付结束后更新为已使用
     * @param orderNo
     * @param tradeState
     * @param openId
     * @param transactionId 微信支付订单号
     */
    @Override
    public void paySucessUpdateOrder(String orderNo,String tradeState,String openId,String transactionId){
        log.info("进入更新方法");
        Map<String,Object> map=new HashMap<>();
        map.put("orderNo",orderNo);
        if("SUCCESS".equals(tradeState)){
            SelectPayOrderOutDto  order=orderMapper.selectPayOrderById(orderNo);
            //优惠券->已使用     图书订单->待发货    会员订单->绑定会员   订单的押金金额记录到用户总押金里
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.eq("id",order.getCouponId());
            updateWrapper.set("coupon_status",BookConstant.COUPON_STATUS_USEED);
            couponUserService.update(updateWrapper);
            if(BookConstant.RENT_ORDER.equals(order.getOrderType())) {
                map.put("payStatus", BookConstant.TO_SEND_PAY);
                map.put("wxPayType","cash");//现金支付
                map.put("orderId",transactionId);//微信支付订单号
            }else if(BookConstant.CARD_ORDER.equals(order.getOrderType())){
                map.put("payStatus",BookConstant.FINISHED_PAY);
                CardOrderDetailDto cardData=payOrderDetailMapper.selectCardByOrderNo(order.getOrderNo());
                memCardService.bindingMemberCard(cardData.getGoodId(),openId);
            }
            orderMapper.updatePayOrderStatus(map);
            //更新押金
            Map<String,Object> userMap=new HashMap<>();
            userMap.put("openId",openId);
            userMap.put("gold",order.getCashPledge());
            userMapper.updateUserGold(userMap);
        }
    }






}
